package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.SearchService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import lombok.RequiredArgsConstructor;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-12 12:51
 */
@SuppressWarnings("all")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/list")
public class ListApiController {

	private final SearchService searchService;

	private final ElasticsearchRestTemplate elasticsearchRestTemplate;

	@GetMapping("/inner/createIndex")
	public Result createIndex() {
		elasticsearchRestTemplate.createIndex(Goods.class);
		elasticsearchRestTemplate.putMapping(Goods.class);
		return Result.ok();
	}

	/**
	 * 商品上架
	 */
	@GetMapping("/inner/upperGoods/{skuId}")
	public Result upperGoods(@PathVariable Long skuId) {
		searchService.upperGoods(skuId);
		return Result.ok();
	}

	/**
	 * 商品下架
	 */
	@GetMapping("/inner/lowerGoods/{skuId}")
	public Result lowerGoods(@PathVariable Long skuId) {
		searchService.lowerGoods(skuId);
		return Result.ok();
	}

	/**
	 * 更新商品热度
	 */
	@GetMapping("/inner/incrHotScore/{skuId}")
	public Result incrHotScore(@PathVariable Long skuId) {
		searchService.incrHotScore(skuId);
		return Result.ok();
	}

	/**
	 * 检索商品
	 */
	@PostMapping
	public Result list(@RequestBody SearchParam searchParam) {
		return Result.ok(searchService.search(searchParam));
	}

}
